﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SKDB_Matcher
{
    public partial class CMatchPage : UserControl
    {
        DataTable m_DestTable;
        int m_SelectedRow = -1;

        public CMatchPage()
        {
            InitializeComponent();
        }

        public void SetDestinationTable(DataTable _table)
        {
            m_DestTable = _table;
            foreach (DataColumn column in m_DestTable.Columns)
                gvTables.Rows.Add("", column.ColumnName + " (" + column.DataType.Name + ")");
        }


        private void TableTree_ItemDrag(object sender, ItemDragEventArgs e)
        {
            System.Text.StringBuilder messageBoxCS = new System.Text.StringBuilder();
            if (e.Button == MouseButtons.Left)
            {
                if(((TreeNode)e.Item).Parent != null)
                    DoDragDrop(e.Item, DragDropEffects.Copy);
            }
        }


        private void gvTables_DragDrop(object sender, DragEventArgs e)
        {
            Point s = gvTables.PointToClient(new Point(e.X, e.Y));
            int target = gvTables.HitTest(s.X, s.Y).RowIndex;
            if (target >= 0)
            {
                DataColumn value = (DataColumn)e.Data.GetData(typeof(DataColumn));

                gvTables.Rows[target].Cells[0].Value = value.Table.TableName + "\\" + value.ColumnName + " (" + value.DataType.Name + ")";                 
            }
        }

        private void gvTables_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = e.AllowedEffect;
        }

        public void SetRowValue(DataColumn Target, DataColumn Value, float Matching)
        {
            if (m_DestTable.Columns.Contains(Target.ColumnName))
            {
                int rowId = m_DestTable.Columns.IndexOf(Target);
                if (Value != null)
                    gvTables.Rows[rowId].Cells[0].Value = Value.Table.TableName + "\\" + Value.ColumnName + " (" + Value.DataType.Name + ")" + " [" + ((int)(Matching*100)).ToString() + "%]";
                else
                    gvTables.Rows[rowId].Cells[0].Value = "";
            }

        }

        private void gvTables_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            
        }

        private void gvTables_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                if (e.RowIndex >= 0)
                {
                    alternativeMatchesToolStripMenuItem.Enabled = CMatcher.GetInstance().SuccesfullMatching;

                    contextMenuStrip1.Show(Cursor.Position);
                    m_SelectedRow = e.RowIndex;

                }
            }
        }

        private void alternativeMatchesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            float[] Matchings = null;
            DataColumn[] Cols = CMatcher.GetInstance().GetAllMatches(m_DestTable.Columns[m_SelectedRow], ref Matchings);
            CAlternativeColDlg altDlg = new CAlternativeColDlg(Cols, Matchings);
            if (altDlg.ShowDialog() == DialogResult.OK)
            {
                DataColumn Selected = altDlg.GetSelectedCol();
                float Matching = altDlg.GetSelectedMatching();
                SetRowValue(m_DestTable.Columns[m_SelectedRow], Selected, Matching);
            }
        }

    }
}
